package com.eascs.admin.o2o.loan.notify.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.admin.o2o.submitted.entity.O2oLoanSubmittedEntity;
import com.eascs.admin.o2o.submitted.serviceimpl.O2oLoanSubmittedServiceImple;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

public class AJBSMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(AJBSMsgNotifyServiceImpl.class);
	@Autowired
	private O2oLoanSubmittedServiceImple o2oLoanSubmittedServiceImple;

	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply) {
		log.info("案件报送流程通知:[{}]", notifyParam.getReferId());
		ModelResult<O2oLoanapplyEntity> modelResult = new ModelResult<O2oLoanapplyEntity>();
		O2oLoanSubmittedEntity o2oLoanSubmittedEntity = o2oLoanSubmittedServiceImple.getEntityById(notifyParam.getReferId());
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {
			o2oLoanSubmittedEntity.setFlowEndTime(new Date());
			o2oLoanSubmittedEntity.setFlowStatus(StatusContexts.O2O_BANK_CLO);
			int i = o2oLoanSubmittedServiceImple.update(o2oLoanSubmittedEntity);
			if (i > 0) {
				log.info("案件报送流程通知 更新成功:[{}]", notifyParam.getReferId());
				modelResult.withModel(loanapply);
			} else {
				log.info("更新O2oLoanSubmittedEntity案件报送失败,流程id:[{}]", notifyParam.getReferId());
				return new ModelResult<>("workflow.notify.insert.error", "财务审核流程通过通知失败");
			}
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {
			o2oLoanSubmittedEntity.setFlowStatus(StatusContexts.O2O_NEW);
			int i = o2oLoanSubmittedServiceImple.update(o2oLoanSubmittedEntity);
			if (i > 0) {
				log.info("案件报送流程通知 更新成功:[{}]", notifyParam.getReferId());
				modelResult.withModel(loanapply);
			} else {
				log.info("更新O2oLoanSubmittedEntity案件报送失败,流程id:[{}]", notifyParam.getReferId());
				return new ModelResult<>("workflow.notify.insert.error", "财务审核流程通过通知失败");
			}
		}
		modelResult.withModel(loanapply);
		return modelResult;
	}

	@Override
	public Boolean checkReturn() {
		return Boolean.TRUE;
	}

	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add("CASE_SUBMIT");
		return list;
	}

}
